﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Domain;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using System.Data;
using SMTC.Model.Model;

namespace SMTC.Dao.BU
{
    public class BUProccessDao : BaseDao
    {
        /// <summary>
        /// 插入bu信息
        /// </summary>
        private const string SQL_INSERT_BU = @"INSERT INTO dbo.SMT_BU (
	                                                BUID,
	                                                BUName,
	                                                PlantID,
	                                                BUCapacity,
                                                    ModifyPerson,
                                                    ModifyDateTime
                                                ) VALUES ( 
	                                                /* BUID - uniqueidentifier */ @BUID,
	                                                /* BUName - nvarchar(50) */ @BUName,
	                                                /* PlantID - uniqueidentifier */ NULL,
	                                                /* BUCapacity - int */ @BUCapacity,
                                                     @ModifyPerson,
                                                     GETDATE()) ";
        /// <summary>
        /// 更新BU和生产线的关系
        /// </summary>
        private const string SQL_INSERT_BU_LINE = @"UPDATE dbo.SMT_Line SET BUID = @BUID
                                                    WHERE LineID = @LineID";
        /// <summary>
        /// 删除BU
        /// </summary>
        private const string SQL_DELETE_BU = @"DELETE dbo.SMT_BU WHERE BUID = @BUID";
        /// <summary>
        /// 删除BU和line的关系
        /// </summary>
        private const string SQL_DELETE_BU_LINE = @"UPDATE dbo.SMT_Line SET BUID = NULL WHERE BUID = @BUID";
        /// <summary>
        /// 更新bu的信息
        /// </summary>
        private const string SQL_UPDATE_BU = @"UPDATE dbo.SMT_BU 
                                                SET BUName = @BUName,BUCapacity = @BUCapacity,ModifyPerson = @ModifyPerson
                                                WHERE BUID = @BUID";


        /// <summary>
        /// 插入bu
        /// </summary>
        /// <param name="bu"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool InsertBu(SMT_BUModel bu, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUID",bu.BUID)
               ,DBparameterBuild.Build("@BUName",bu.BUName)
               ,DBparameterBuild.Build("@BUCapacity",bu.BUCapacity)
               ,DBparameterBuild.Build("@ModifyPerson",bu.ModifyPerson)

            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_BU, parameters, dbt) > 0;
        }
        /// <summary>
        /// 更新bu和line的关系
        /// </summary>
        /// <param name="line"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool InsertBusLine(SMT_LineModel line, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUID",line.BUID)
               ,DBparameterBuild.Build("@LineID",line.LineID)

            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_BU_LINE, parameters, dbt) > 0;
        }
        /// <summary>
        /// 删除BU
        /// </summary>
        /// <param name="line"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool DeleteBu(string BUID, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUID",BUID)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_BU, parameters, dbt) > 0;
        }
        /// <summary>
        /// 删除BU和line的关系
        /// </summary>
        /// <param name="line"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool DeleteBusline(string BUID, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUID",BUID)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_BU_LINE, parameters, dbt) > 0;
        }
        /// <summary>
        /// 更新bu信息
        /// </summary>
        /// <param name="bu"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool UpdateBU(SMT_BUModel bu, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@BUID",bu.BUID)
               ,DBparameterBuild.Build("@BUName",bu.BUName)
               ,DBparameterBuild.Build("@BUCapacity",bu.BUCapacity)
               ,DBparameterBuild.Build("@ModifyPerson",bu.ModifyPerson)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_UPDATE_BU, parameters, dbt) > 0;
        }


    }
}
